package com.ruoyi.project.emr.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.project.emr.service.IEmrMedicalRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.emr.domain.EmrPatient;
import com.ruoyi.project.emr.service.IEmrPatientService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 患者Controller
 * @author gzu
 * @date 2023-07-21
 */
@RestController
@RequestMapping("/emr/patient")
public class EmrPatientController extends BaseController
{
    @Autowired
    private IEmrPatientService emrPatientService;

    @Autowired
    private IEmrMedicalRecordService emrMedicalRecordService;

    /**
     * 查询患者列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmrPatient emrPatient)
    {
        startPage();
        List<EmrPatient> list = emrPatientService.selectEmrPatientList(emrPatient);
        for(int i = 0; i < list.size(); ++i) {
            if(list.get(i).getGender() == null) {
                continue;
            }
            if (list.get(i).getGender().equals("M")) {
                list.get(i).setGender("男");
            } else if (list.get(i).getGender().equals("F")) {
                list.get(i).setGender("女");
            } else {
                list.get(i).setGender("未知");
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取患者详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emrPatientService.selectEmrPatientById(id));
    }

    /**
     * 新增患者
     */
    @PostMapping
    public AjaxResult add(@RequestBody EmrPatient emrPatient)
    {
        if(emrPatient.getGender().equals("男")) {
            emrPatient.setGender("M");
        } else if (emrPatient.getGender().equals("女")) {
            emrPatient.setGender("F");
        } else{
            emrPatient.setGender("F");
        }
        return toAjax(emrPatientService.insertEmrPatient(emrPatient));
    }

    /**
     * 修改患者
     */
    @PutMapping
    public AjaxResult edit(@RequestBody EmrPatient emrPatient)
    {
        if(emrPatient.getGender().equals("男")) {
            emrPatient.setGender("M");
        } else if (emrPatient.getGender().equals("女")) {
            emrPatient.setGender("F");
        } else{
            emrPatient.setGender("F");
        }
        return toAjax(emrPatientService.updateEmrPatient(emrPatient));
    }

    /**
     * 删除患者
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emrPatientService.deleteEmrPatientByIds(ids));
    }
}
